home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Count lines in file?
- Date: Wed, 03 Jan 96 13:41:45 GMT
- Organization: none
- Message-ID: <820676505snz@genesis.demon.co.uk>
- References: <4cd4lm$j62@fountain.mindlink.net>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4cd4lm$j62@fountain.mindlink.net>
- genew@mindlink.bc.ca "Gene Wirchenko" writes:
-
- >Lawrence Kirby <fred@genesis.demon.co.uk> wrote:
- >
- >[Attack of the giant scissors]
- >
- >>>Oh, before someone else points it out: One problem with getc might be if
- >>>the last line of the program doesn't have a terminating newline
- >>>character, i.e. it terminates with the eof. This could cause the count
- >>>to be off by one in such cases.
- >
- >>It depends on your definition of a 'line'. It is reasonable to say is that
- >>of the final sequence of characters is not terminated by '\n' it is not
- >>a valid line.
- >
- >[snip]
- >
- > You'd run into trouble on a UNIX system where the final line ends
- >with the end of file. (In my limited experience: UNIX experts, if I'm
- >wrong, just smile to yourself and read on.)
-
- $ echo 'test line' | wc -l
- 1
- $ echo -n 'test line' | wc -l
- 0
-
- (echo -n writes the text without a terminating newline, wc -l is the standard
- Unix command for counting lines in a file).
-
- Well formed lines end with '\n' in Unix.
-
- > Cmm nes on, Mr. Kirby!
- >
- > It depends on your definition of a "line". It is reasonable to
- >say that if <implementation defined condition>, it is not a line.
-
- Exactly. All I'm saying is that counting '\n's is as valid a way of counting
- lines as any other unless you have specific information to the contrary
- concerning a particular implementation.
-
- We should be talking about counting lines from streams opened in text
- mode. In that case the standard says "Whether the last line requires
- a terminating new-line character is implementation-defined" (as we both
- seem to be saying).
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-